home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / ToolTipManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  9.7 KB  |  396 lines

  1. package javax.swing;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Frame;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.FocusListener;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseMotionListener;
  15. import java.util.EventObject;
  16.  
  17. public class ToolTipManager extends MouseAdapter implements MouseMotionListener {
  18.    Timer enterTimer = new Timer(750, new insideTimerAction(this));
  19.    Timer exitTimer;
  20.    Timer insideTimer;
  21.    String toolTipText;
  22.    Point preferredLocation;
  23.    JComponent insideComponent;
  24.    MouseEvent mouseEvent;
  25.    boolean showImmediately;
  26.    static final ToolTipManager sharedInstance = new ToolTipManager();
  27.    Popup tipWindow;
  28.    JToolTip tip;
  29.    private Rectangle popupRect = null;
  30.    private Rectangle popupFrameRect = null;
  31.    boolean enabled = true;
  32.    boolean mouseAboveToolTip = false;
  33.    private boolean tipShowing = false;
  34.    private long timerEnter = 0L;
  35.    private KeyStroke postTip;
  36.    private KeyStroke hideTip;
  37.    private ActionListener postTipAction;
  38.    private ActionListener hideTipAction;
  39.    private FocusListener focusChangeListener = null;
  40.    protected boolean lightWeightPopupEnabled = true;
  41.    protected boolean heavyWeightPopupEnabled = false;
  42.  
  43.    ToolTipManager() {
  44.       this.enterTimer.setRepeats(false);
  45.       this.exitTimer = new Timer(500, new outsideTimerAction(this));
  46.       this.exitTimer.setRepeats(false);
  47.       this.insideTimer = new Timer(4000, new stillInsideTimerAction(this));
  48.       this.insideTimer.setRepeats(false);
  49.       this.postTip = KeyStroke.getKeyStroke(112, 2);
  50.       this.postTipAction = new 1(this);
  51.       this.hideTip = KeyStroke.getKeyStroke(27, 0);
  52.       this.hideTipAction = new 2(this);
  53.    }
  54.  
  55.    // $FF: synthetic method
  56.    static FocusListener access$0(ToolTipManager var0) {
  57.       return var0.focusChangeListener;
  58.    }
  59.  
  60.    // $FF: synthetic method
  61.    static FocusListener access$1(ToolTipManager var0) {
  62.       return var0.createFocusChangeListener();
  63.    }
  64.  
  65.    // $FF: synthetic method
  66.    static void access$2(ToolTipManager var0, FocusListener var1) {
  67.       var0.focusChangeListener = var1;
  68.    }
  69.  
  70.    private FocusListener createFocusChangeListener() {
  71.       return new 3(this);
  72.    }
  73.  
  74.    static Frame frameForComponent(Component var0) {
  75.       while(!(var0 instanceof Frame)) {
  76.          var0 = ((Component)var0).getParent();
  77.       }
  78.  
  79.       return (Frame)var0;
  80.    }
  81.  
  82.    public int getDismissDelay() {
  83.       return this.insideTimer.getInitialDelay();
  84.    }
  85.  
  86.    private int getHeightAdjust(Rectangle var1, Rectangle var2) {
  87.       return var2.y >= var1.y && var2.y + var2.height <= var1.y + var1.height ? 0 : var2.y + var2.height - (var1.y + var1.height) + 5;
  88.    }
  89.  
  90.    public int getInitialDelay() {
  91.       return this.enterTimer.getInitialDelay();
  92.    }
  93.  
  94.    private int getPopupFitHeight(Rectangle var1, Component var2) {
  95.       if (var2 != null) {
  96.          for(Container var3 = var2.getParent(); var3 != null; var3 = ((Component)var3).getParent()) {
  97.             if (var3 instanceof JFrame || var3 instanceof JDialog || var3 instanceof JWindow) {
  98.                return this.getHeightAdjust(((Component)var3).getBounds(), var1);
  99.             }
  100.  
  101.             if (var3 instanceof JApplet || var3 instanceof JInternalFrame) {
  102.                if (this.popupFrameRect == null) {
  103.                   this.popupFrameRect = new Rectangle();
  104.                }
  105.  
  106.                Point var4 = ((Component)var3).getLocationOnScreen();
  107.                this.popupFrameRect.setBounds(var4.x, var4.y, ((Component)var3).getBounds().width, ((Component)var3).getBounds().height);
  108.                return this.getHeightAdjust(this.popupFrameRect, var1);
  109.             }
  110.          }
  111.       }
  112.  
  113.       return 0;
  114.    }
  115.  
  116.    private int getPopupFitWidth(Rectangle var1, Component var2) {
  117.       if (var2 != null) {
  118.          for(Container var3 = var2.getParent(); var3 != null; var3 = ((Component)var3).getParent()) {
  119.             if (var3 instanceof JFrame || var3 instanceof JDialog || var3 instanceof JWindow) {
  120.                return this.getWidthAdjust(((Component)var3).getBounds(), var1);
  121.             }
  122.  
  123.             if (var3 instanceof JApplet || var3 instanceof JInternalFrame) {
  124.                if (this.popupFrameRect == null) {
  125.                   this.popupFrameRect = new Rectangle();
  126.                }
  127.  
  128.                Point var4 = ((Component)var3).getLocationOnScreen();
  129.                this.popupFrameRect.setBounds(var4.x, var4.y, ((Component)var3).getBounds().width, ((Component)var3).getBounds().height);
  130.                return this.getWidthAdjust(this.popupFrameRect, var1);
  131.             }
  132.          }
  133.       }
  134.  
  135.       return 0;
  136.    }
  137.  
  138.    public int getReshowDelay() {
  139.       return this.exitTimer.getInitialDelay();
  140.    }
  141.  
  142.    private int getWidthAdjust(Rectangle var1, Rectangle var2) {
  143.       return var2.x >= var1.x && var2.x + var2.width <= var1.x + var1.width ? 0 : var2.x + var2.width - (var1.x + var1.width) + 5;
  144.    }
  145.  
  146.    void hideTipWindow() {
  147.       if (this.tipWindow != null) {
  148.          this.tipWindow.removeMouseListener(this);
  149.          this.tipWindow.hide();
  150.          this.tipWindow = null;
  151.          this.tipShowing = false;
  152.          this.timerEnter = 0L;
  153.          this.tip.getUI().uninstallUI(this.tip);
  154.          this.tip = null;
  155.          this.insideTimer.stop();
  156.       }
  157.  
  158.    }
  159.  
  160.    public boolean isEnabled() {
  161.       return this.enabled;
  162.    }
  163.  
  164.    public boolean isLightWeightPopupEnabled() {
  165.       return this.lightWeightPopupEnabled;
  166.    }
  167.  
  168.    public void mouseDragged(MouseEvent var1) {
  169.    }
  170.  
  171.    public void mouseEntered(MouseEvent var1) {
  172.       if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 200L) {
  173.          if (((EventObject)var1).getSource() != this.tipWindow) {
  174.             JComponent var2 = (JComponent)((EventObject)var1).getSource();
  175.             this.toolTipText = var2.getToolTipText(var1);
  176.             this.preferredLocation = var2.getToolTipLocation(var1);
  177.             this.exitTimer.stop();
  178.             Point var3 = var1.getPoint();
  179.             if (var3.x >= 0 && var3.x < var2.getWidth() && var3.y >= 0 && var3.y < var2.getHeight()) {
  180.                if (this.insideComponent != null) {
  181.                   this.enterTimer.stop();
  182.                   this.insideComponent = null;
  183.                }
  184.  
  185.                ((Component)var2).addMouseMotionListener(this);
  186.                this.insideComponent = var2;
  187.                if (this.tipWindow != null) {
  188.                   if (this.heavyWeightPopupEnabled) {
  189.                      return;
  190.                   }
  191.  
  192.                   this.mouseEvent = var1;
  193.                   if (this.showImmediately) {
  194.                      this.showTipWindow();
  195.                   } else {
  196.                      this.enterTimer.start();
  197.                   }
  198.                }
  199.  
  200.             }
  201.          }
  202.       }
  203.    }
  204.  
  205.    public void mouseExited(MouseEvent var1) {
  206.       if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 200L) {
  207.          boolean var2 = true;
  208.          if (((EventObject)var1).getSource() == this.tipWindow) {
  209.             Container var3 = this.insideComponent.getTopLevelAncestor();
  210.             Rectangle var4 = this.tipWindow.getBounds();
  211.             Point var5 = var1.getPoint();
  212.             var5.x += var4.x;
  213.             var5.y += var4.y;
  214.             var4 = ((Component)var3).getBounds();
  215.             var5.x -= var4.x;
  216.             var5.y -= var4.y;
  217.             var5 = SwingUtilities.convertPoint((Component)null, var5, this.insideComponent);
  218.             if (var5.x >= 0 && var5.x < this.insideComponent.getWidth() && var5.y >= 0 && var5.y < this.insideComponent.getHeight()) {
  219.                var2 = false;
  220.             } else {
  221.                var2 = true;
  222.             }
  223.          } else if (((EventObject)var1).getSource() == this.insideComponent && this.tipWindow != null) {
  224.             Point var6 = SwingUtilities.convertPoint(this.insideComponent, var1.getPoint(), (Component)null);
  225.             Rectangle var8 = this.insideComponent.getTopLevelAncestor().getBounds();
  226.             var6.x += var8.x;
  227.             var6.y += var8.y;
  228.             var8 = this.tipWindow.getBounds();
  229.             if (var6.x >= var8.x && var6.x < var8.x + var8.width && var6.y >= var8.y && var6.y < var8.y + var8.height) {
  230.                var2 = false;
  231.             } else {
  232.                var2 = true;
  233.             }
  234.          }
  235.  
  236.          if (var2) {
  237.             this.enterTimer.stop();
  238.             if (this.insideComponent != null) {
  239.                this.insideComponent.removeMouseMotionListener(this);
  240.             }
  241.  
  242.             this.insideComponent = null;
  243.             this.toolTipText = null;
  244.             this.mouseEvent = null;
  245.             this.hideTipWindow();
  246.             this.exitTimer.start();
  247.          }
  248.  
  249.       }
  250.    }
  251.  
  252.    public void mouseMoved(MouseEvent var1) {
  253.       JComponent var2 = (JComponent)((EventObject)var1).getSource();
  254.       String var3 = var2.getToolTipText(var1);
  255.       Point var4 = var2.getToolTipLocation(var1);
  256.       if (var3 == null && var4 == null) {
  257.          this.toolTipText = null;
  258.          this.preferredLocation = null;
  259.          this.mouseEvent = null;
  260.          this.hideTipWindow();
  261.          this.enterTimer.stop();
  262.          this.exitTimer.start();
  263.       } else {
  264.          this.mouseEvent = var1;
  265.          if ((var3 != null && var3.equals(this.toolTipText) || var3 == null) && (var4 != null && var4.equals(this.preferredLocation) || var4 == null)) {
  266.             if (this.tipWindow != null) {
  267.                this.insideTimer.restart();
  268.             } else {
  269.                this.enterTimer.restart();
  270.             }
  271.          } else {
  272.             this.toolTipText = var3;
  273.             this.preferredLocation = var4;
  274.             if (this.showImmediately) {
  275.                this.hideTipWindow();
  276.                this.showTipWindow();
  277.             } else {
  278.                this.enterTimer.restart();
  279.             }
  280.          }
  281.       }
  282.  
  283.    }
  284.  
  285.    public void mousePressed(MouseEvent var1) {
  286.       this.hideTipWindow();
  287.       this.enterTimer.stop();
  288.       this.showImmediately = false;
  289.    }
  290.  
  291.    public void registerComponent(JComponent var1) {
  292.       ((Component)var1).removeMouseListener(this);
  293.       ((Component)var1).addMouseListener(this);
  294.       var1.registerKeyboardAction(this.postTipAction, this.postTip, 0);
  295.       var1.registerKeyboardAction(this.hideTipAction, this.hideTip, 0);
  296.    }
  297.  
  298.    public void setDismissDelay(int var1) {
  299.       this.insideTimer.setInitialDelay(var1);
  300.    }
  301.  
  302.    public void setEnabled(boolean var1) {
  303.       this.enabled = var1;
  304.       if (!var1) {
  305.          this.hideTipWindow();
  306.       }
  307.  
  308.    }
  309.  
  310.    public void setInitialDelay(int var1) {
  311.       this.enterTimer.setInitialDelay(var1);
  312.    }
  313.  
  314.    /** @deprecated */
  315.    public void setLightWeightPopupEnabled(boolean var1) {
  316.       this.lightWeightPopupEnabled = var1;
  317.    }
  318.  
  319.    public void setReshowDelay(int var1) {
  320.       this.exitTimer.setInitialDelay(var1);
  321.    }
  322.  
  323.    public static ToolTipManager sharedInstance() {
  324.       return sharedInstance;
  325.    }
  326.  
  327.    void showTipWindow() {
  328.       if (this.insideComponent != null && this.insideComponent.isShowing()) {
  329.          if (this.enabled) {
  330.             Point var2 = this.insideComponent.getLocationOnScreen();
  331.             Dimension var3 = Toolkit.getDefaultToolkit().getScreenSize();
  332.             Point var4 = new Point();
  333.             this.hideTipWindow();
  334.             this.tip = this.insideComponent.createToolTip();
  335.             this.tip.setTipText(this.toolTipText);
  336.             Dimension var1 = this.tip.getPreferredSize();
  337.             if (this.insideComponent.getRootPane() == null) {
  338.                this.tipWindow = new WindowPopup(this, frameForComponent(this.insideComponent), this.tip, var1);
  339.                this.heavyWeightPopupEnabled = true;
  340.             } else if (this.lightWeightPopupEnabled) {
  341.                this.heavyWeightPopupEnabled = false;
  342.                this.tipWindow = new JPanelPopup(this, this.tip, var1);
  343.             } else {
  344.                this.heavyWeightPopupEnabled = false;
  345.                this.tipWindow = new PanelPopup(this, this.tip, var1);
  346.             }
  347.  
  348.             this.tipWindow.addMouseListener(this);
  349.             if (this.preferredLocation != null) {
  350.                var4.x = var2.x + this.preferredLocation.x;
  351.                var4.y = var2.y + this.preferredLocation.y;
  352.             } else {
  353.                var4.x = var2.x + this.mouseEvent.getX();
  354.                var4.y = var2.y + this.mouseEvent.getY() + 20;
  355.                if (var4.x + var1.width > var3.width) {
  356.                   var4.x -= var1.width;
  357.                }
  358.  
  359.                if (var4.y + var1.height > var3.height) {
  360.                   var4.y -= var1.height + 20;
  361.                }
  362.             }
  363.  
  364.             if (!this.heavyWeightPopupEnabled) {
  365.                if (this.popupRect == null) {
  366.                   this.popupRect = new Rectangle();
  367.                }
  368.  
  369.                this.popupRect.setBounds(var4.x, var4.y, this.tipWindow.getBounds().width, this.tipWindow.getBounds().height);
  370.                int var5 = this.getPopupFitHeight(this.popupRect, this.insideComponent);
  371.                int var6 = this.getPopupFitWidth(this.popupRect, this.insideComponent);
  372.                if (var5 > 0) {
  373.                   var4.y -= var5;
  374.                }
  375.  
  376.                if (var6 > 0) {
  377.                   var4.x -= var6;
  378.                }
  379.             }
  380.  
  381.             this.tipWindow.show(this.insideComponent, var4.x, var4.y);
  382.             this.insideTimer.start();
  383.             this.timerEnter = System.currentTimeMillis();
  384.             this.tipShowing = true;
  385.          }
  386.  
  387.       }
  388.    }
  389.  
  390.    public void unregisterComponent(JComponent var1) {
  391.       ((Component)var1).removeMouseListener(this);
  392.       var1.unregisterKeyboardAction(this.postTip);
  393.       var1.unregisterKeyboardAction(this.hideTip);
  394.    }
  395. }
  396.